using System.Collections;
using System.Collections.Generic;
using System.IO;
using System.Text;
using UnityEditor;

namespace SIM
{
    public class FbxAnimListPostprocessor : AssetPostprocessor
    {
        public void OnPreprocessModel()
        {
            if (Path.GetExtension(assetPath).ToLower() == ".fbx"
                && !assetPath.Contains("@"))
            {
                try
                {
                    string fileAnim;
                    fileAnim = assetPath;
                    string ClipText = Path.ChangeExtension(fileAnim, ".txt");
                    StreamReader file = new StreamReader(ClipText);
                    string sAnimList = file.ReadToEnd();
                    StringBuilder stringBuilder = new StringBuilder(sAnimList);
                    file.Close();
                    System.Collections.ArrayList List = new ArrayList();
                    ParseAnimFile(sAnimList, ref List);
                    ModelImporter modelImporter = assetImporter as ModelImporter;

                    modelImporter.clipAnimations = ReadAndCopyAnima(modelImporter.clipAnimations,
                        (ModelImporterClipAnimation[])
                        List.ToArray(typeof(ModelImporterClipAnimation)));
                }
                catch
                {
                }
            }
        }

        ModelImporterClipAnimation[] ReadAndCopyAnima(ModelImporterClipAnimation[] currentArray,
            ModelImporterClipAnimation[] newArray)
        {
            List<ModelImporterClipAnimation> clipList = new List<ModelImporterClipAnimation>();
            for (int i = 0; i < newArray.Length; i++)
            {
                if (i < currentArray.Length)
                {
                    var currentclip = currentArray[i];
                    var newclip = newArray[i];
                    currentclip.firstFrame = newclip.firstFrame;
                    currentclip.lastFrame = newclip.lastFrame;
                    currentclip.loopTime = newclip.loopTime;
                    currentclip.loop = newclip.loopTime;
                    currentclip.wrapMode = newclip.loopTime == true ? UnityEngine.WrapMode.Loop : UnityEngine.WrapMode.Default;
                    currentclip.loopPose = newclip.loopTime;
                    currentclip.name = newclip.name;
                    clipList.Add(currentclip);
                }
                else
                {
                    clipList.Add(newArray[i]);
                }
            }

            return clipList.ToArray();
        }

        void ParseAnimFile(string sAnimList, ref ArrayList List)
        {
            string[] allData = sAnimList.Split("\r\n");

            foreach (var Data in allData)
            {
                if (!string.IsNullOrEmpty(Data))
                {
                    string[] param = Data.Split("\t");

                    ModelImporterClipAnimation clip = new ModelImporterClipAnimation();

                    clip.firstFrame = System.Convert.ToInt32(param[0].Split("-")[0]);
                    clip.lastFrame = System.Convert.ToInt32(param[0].Split("-")[1]);
                    clip.loopTime = param[1] == "loop";
                    clip.name = param[2];
                    List.Add(clip);
                }
            }
        }
    }
}